
var app = getApp();
const $ = require('../../utils/utils.js')
const config = require('../../config.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        topIsLight: false,
        middleIsLight: false,
        topLightUrl: "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/top-light-style.png",
        middleLightUrl: "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/middle-light-hit-style-1.png",
        btnUrl: "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/button_on.png",
        centerCatUrl: "../../img/cat0.png",
        tapBtnImgClass: "tapBtn-img-nor",
        middle_count_class: "middle-textArea-coun",
        animationData: {},
        isShowTipImg: true,
        isShowResult: false,
        seconds: 15,
        hitCount: 0,
        cashCount: 0,
        tryCount: 1,
        submitCount: 0
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.changeTopLight();
        this.changeMiddleLight();
        this.createPlayAudio();
        this.createPlayBackAudio()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.bgAudioCtx.destroy();
        this.audioCtx.destroy();
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.bgAudioCtx.destroy();
        this.audioCtx.destroy();
        if (this.data.tryCount == 2 && this.data.hitCount > 0) {
            this.tapGetCashAction()
        }

    },


    /************************
     * 业务逻辑相关
     * **********************
     */

    // 点击再来一次
    tapAgainAction: function () {
        this.setData({
            seconds: 15,
            hitCount: 0,
            cashCount: 0,
            isShowResult: false,
            tryCount: 2
        })
    },

    // 点击获取奖励
    tapGetCashAction: function (res) {
        var that = this;
        console.log("tapGetCashAction", that.data.tryCount)
        if (that.data.tryCount <= 2) {
            wx.setStorage({
                key: 'hitCount',
                data: that.data.hitCount,
                success: function () {
                    app.saveAttendorInfo();
                }
            });
        }
    },

/*************************************
 * 动效相关
 ************************************/

    // 中部闪光灯
    changeMiddleLight: function () {
        this.timer = setInterval(function () {
            if (this.middleIsLight) {
                this.middleIsLight = false;
                this.setData({
                    middleLightUrl: 'http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/middle-light-hit-style-2.png',
                })
            } else {
                this.middleIsLight = true;
                this.setData({
                    middleLightUrl: 'http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/middle-light-hit-style-1.png',
                })
            }
        }.bind(this), 500)
    },


    // 顶部闪光灯
    changeTopLight: function () {
        this.timer = setInterval(function () {
            if (this.topIsLight) {
                this.topIsLight = false;
                this.setData({
                    topLightUrl: 'http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/top-light-style.png',
                })
            } else {
                this.topIsLight = true;
                this.setData({
                    topLightUrl: 'http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/top-light-style-1.png',
                })
            }
        }.bind(this), 300)
    },

    // 倒计时
    changeSecond: function () {
        this.secondTimer = setInterval(function () {
            var secondCount = this.data.hitCount / (15 - this.data.seconds);
            if (secondCount <= 8 && secondCount > 5) {
                this.setData({
                    centerCatUrl: "../../img/cat2.png",
                })
            } else if (secondCount > 8) {
                this.setData({
                    centerCatUrl: "../../img/cat3.png",
                })
            }

            if (this.data.seconds < 1) {
                var currentCash = this.data.hitCount * 0.05;
                this.setData({
                    isShowResult: true,
                    cashCount: currentCash.toFixed(2),
                })

                clearTimeout(this.secondTimer);
                return;
            } else {
                var current = this.data.seconds - 1;
                this.setData({
                    seconds: current,
                })
            }

        }.bind(this), 1000)
    },

    // 点数增加
    changeHitCount: function () {
        this.audioCtx.stop();
        this.audioCtx.play();
        if (this.data.hitCount == 0) {
            this.changeSecond();
            this.setData({
                centerCatUrl: "../../img/cat1.png"
            })
        }

        var hit = this.data.hitCount;

        if (this.data.hitCount > 225) {
            return;
        }
        var animation = wx.createAnimation({
            duration: 50,
            timingFunction: "linear",
        })

        this.animation = animation
        animation.scale(1.2).step();
        animation.scale(0.8).step();

        if (hit % 5 == 3) {
            hit = hit + this.getRandomCount(2, 5);
        } else {
            hit += 1;
        }

        hit = hit > 225 ? 225 : hit;

        this.setData({
            hitCount: hit,
            animationData: animation.export(),
            isShowTipImg: false
        })

    },


    /**************
     * 
     * 游戏相关
     * 
     * 
     * 
     */

    touchStartAction: function () {
        this.setData({
            tapBtnImgClass: "tapBtn-img-sel",
            btnUrl: "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/button_down.png"
        })
    },

    touchEndAction: function () {
        this.setData({
            tapBtnImgClass: "tapBtn-img-nor",
            btnUrl: "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/button_on.png"
        })
    },

    // 获取随机数
    getRandomCount: function (min, max) {
        var Range = max - min;
        var Rand = Math.random();
        return (min + Math.round(Rand * Range));
    },


    createPlayAudio: function () {
        this.audioCtx = wx.createInnerAudioContext()
        this.audioCtx.src = "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/audio/coin.mp3";
    },

    createPlayBackAudio: function () {
        this.bgAudioCtx = wx.createInnerAudioContext()
        this.bgAudioCtx.autoplay = true;
        this.bgAudioCtx.src = "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/audio/grabCard-bg.mp3";
        var that = this;
        this.bgAudioCtx.onEnded(() => {
            that.bgAudioCtx.play();
        });
    },


})